package com.bysc.cmsservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.cmsservice.entity.StudyType;
import com.bysc.cmsservice.entity.vo.StudyTypeVo;
import com.bysc.cmsservice.service.StudyTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-05-28
 */
@RestController
@Api(tags = "学习资料类型管理")
@RequestMapping("/studyservice/study-type")
public class StudyTypeController {

    @Autowired
    private StudyTypeService studyTypeService;


    @ApiOperation(value = "新增学习资料类型")
    @PostMapping("addStudyType")
    public Results addStudyType(@ApiParam(name = "StudyTypeInfo", value = "学习资料类型", required = true) @RequestBody StudyType studyType) {
        try {
            studyTypeService.save(studyType);
            return Results.ok().message(MessageConstant.ADD_STUDYTYPE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.ADD_STUDYTYPE_FAIL);
        }
    }

    @ApiOperation(value = "根据id删除类型")
    @DeleteMapping("deleteStudyTypeById/{id}")
    public Results deleteStudyTypeById(@ApiParam(name = "id", value = "学习资料id", required = true) @PathVariable String id) {
        try {
            StudyType studyType = studyTypeService.getById(id);
            if (studyType != null) {
                studyTypeService.removeById(id);
            }
            return Results.ok().message(MessageConstant.DELETE_STUDYTYPE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.DELETE_STUDYTYPE_FAIL);
        }
    }
    @ApiOperation(value = "根据id查询子类型")
    @DeleteMapping("getStudyTypeById/{id}")
    public Results getStudyTypeById(@ApiParam(name = "id", value = "学习资料id", required = true) @PathVariable String id) {
        try {
            StudyType studyType = studyTypeService.getById(id);
            return Results.ok().data("studyType",studyType).message(MessageConstant.QUERY_STUDYTYPE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_STUDYTYPE_FAIL);
        }
    }

    @ApiOperation(value = "根据id编辑学习资料类型")
    @PutMapping("updateStudyTypeById/{id}")
    public Results updateStudyTypeById(@ApiParam(name = "StudyTypeInfo", value = "学习资料类型", required = true) @RequestBody StudyType studyType) {
        try {
            studyTypeService.updateById(studyType);
            return Results.ok().message(MessageConstant.EDIT_STUDYTYPE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.EDIT_STUDYTYPE_FAIL);
        }
    }

    @ApiOperation(value = "查询所有学习资料类型")
    @PostMapping("getAllStudyType/{page}/{limit}")
    public Results getAllStudyType() {
        try {
            QueryWrapper<StudyType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",0);
            queryWrapper.orderByDesc("sort");
            List<StudyType> list = studyTypeService.list(queryWrapper);
            ArrayList<StudyTypeVo> studyTypeVos = new ArrayList<StudyTypeVo>();
            for (StudyType studyType : list) {
                StudyTypeVo studyTypeVo = new StudyTypeVo();
                QueryWrapper<StudyType> wrapper = new QueryWrapper<>();
                wrapper.eq("parent_id",studyType.getId());
                wrapper.orderByDesc("sort");
                studyTypeVo.setParentId(studyType.getId());
                studyTypeVo.setStudyTypeList(studyTypeService.list(wrapper));
                studyTypeVo.setPName(studyType.getName());
                studyTypeVo.setSort(studyType.getSort());
                studyTypeVos.add(studyTypeVo);
            }
            return Results.ok().data("tree",studyTypeVos).message(MessageConstant.QUERY_STUDYTYPE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_STUDYTYPE_FAIL);
        }
    }
}

